home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / RKMLibsPrgs / keymap / mapansi.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  6KB  |  188 lines

  1. ;/* mapansi.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 mapansi.c
  3. Blink FROM LIB:c.o,mapansi.o TO mapansi LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5. */
  6.  
  7. /*
  8. Copyright (c) 1992 Commodore-Amiga, Inc.
  9.  
  10. This example is provided in electronic form by Commodore-Amiga, Inc. for
  11. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  12. published by Addison-Wesley (ISBN 0-201-56774-1).
  13.  
  14. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  15. information on the correct usage of the techniques and operating system
  16. functions presented in these examples.  The source and executable code
  17. of these examples may only be distributed in free electronic form, via
  18. bulletin board or as part of a fully non-commercial and freely
  19. redistributable diskette.  Both the source and executable code (including
  20. comments) must be included, without modification, in any copy.  This
  21. example may not be published in printed form or distributed with any
  22. commercial product.  However, the programming techniques and support
  23. routines set forth in these examples may be used in the development
  24. of original executable software products for Commodore Amiga computers.
  25.  
  26. All other rights reserved.
  27.  
  28. This example is provided "as-is" and is subject to change; no
  29. warranties are made.  All use is at your own risk. No liability or
  30. responsibility is assumed.
  31. */
  32.  
  33. /*
  34. mapansi.c - converts a string to input events using MapANSI() function.
  35.  
  36.             This example will also take the created input events
  37.             and add them to the input stream using the simple
  38.             commodities.library function AddIEvents().  Alternately,
  39.             you could open the input.device and use the input device
  40.             command IND_WRITEEVENT to add events to the input stream.
  41. */
  42.  
  43. #include       <exec/types.h>
  44. #include       <exec/memory.h>
  45. #include       <exec/io.h>
  46. #include       <dos/dos.h>
  47. #include       <devices/input.h>
  48. #include       <devices/inputevent.h>
  49.  
  50. #include       <clib/exec_protos.h>
  51. #include       <clib/dos_protos.h>
  52. #include       <clib/keymap_protos.h>
  53. #include       <clib/commodities_protos.h>
  54.  
  55. #include       <stdio.h>
  56. #include       <stdlib.h>
  57.  
  58. #ifdef LATTICE
  59. int CXBRK(void)  { return(0); }  /* Disable Lattice CTRL/C handling */
  60. void chkabort(void) { return; }  /* really */
  61. #endif
  62.  
  63. struct Library          *KeymapBase = NULL;       /* MapAnsi() function    */
  64. struct Library          *CxBase = NULL;           /* AddIEvents() function */
  65.  
  66. struct InputEvent       *InputEvent = NULL;       /* we'll allocate this */
  67.  
  68. /* prototypes for our program functions */
  69.  
  70. void openall(void);
  71. void closeall(void);
  72. void closeout(UBYTE *errstring, LONG rc);
  73.  
  74.  
  75. void main(int argc, char **argv)
  76. {
  77.     UBYTE               *string;
  78.     UBYTE               *tmp1;
  79.     UBYTE               *tmp2;
  80.     UBYTE               iebuffer[6];            /* Space for two dead keys */
  81.                                                 /* + 1 key + qualifiers    */
  82.     COUNT               i;
  83.     LONG                rc = 0;
  84.  
  85.  
  86.     openall();
  87.  
  88.     string = ";String converted to input events and sent to input device\n";
  89.  
  90.     InputEvent->ie_Class = IECLASS_RAWKEY;
  91.  
  92.     /* Turn each character into an InputEvent */
  93.     tmp1 = string;
  94.  
  95.     while (*tmp1)
  96.     {
  97.         /* Convert one character, use default key map */
  98.         i = MapANSI(tmp1, 1, iebuffer, 3, NULL);
  99.  
  100.         /* Make sure we start without deadkeys */
  101.         InputEvent->ie_Prev1DownCode = InputEvent->ie_Prev1DownQual = 0;
  102.         InputEvent->ie_Prev2DownCode = InputEvent->ie_Prev2DownQual = 0;
  103.  
  104.         tmp2 = iebuffer;
  105.  
  106.         switch (i)
  107.         {
  108.           case -2:
  109.             printf("Internal error\n", NULL);
  110.             rc = RETURN_FAIL;
  111.             break;
  112.  
  113.           case -1:
  114.             printf("Overflow\n", NULL);
  115.             rc = RETURN_FAIL;
  116.             break;
  117.  
  118.           case 0:
  119.             printf("Can't generate code\n", NULL);
  120.             break;
  121.  
  122.           case 3:
  123.             InputEvent->ie_Prev2DownCode = *tmp2++;
  124.             InputEvent->ie_Prev2DownQual = *tmp2++;
  125.             /* FALL THROUGH */
  126.  
  127.           case 2:
  128.             InputEvent->ie_Prev1DownCode = *tmp2++;
  129.             InputEvent->ie_Prev1DownQual = *tmp2++;
  130.             /* FALL THROUGH */
  131.  
  132.           case 1:
  133.             InputEvent->ie_Code = *tmp2++;
  134.             InputEvent->ie_Qualifier = *tmp2;
  135.             break;
  136.         }
  137.  
  138.         if (rc == RETURN_OK)
  139.         {
  140.             /* Send the key down event */
  141.             AddIEvents(InputEvent);
  142.  
  143.             /* Create a key up event */
  144.             InputEvent->ie_Code |= IECODE_UP_PREFIX;
  145.  
  146.             /* Send the key up event */
  147.             AddIEvents(InputEvent);
  148.          }
  149.  
  150.         if (rc != RETURN_OK)
  151.             break;
  152.  
  153.         tmp1++;
  154.     }
  155.  
  156.     closeall();
  157.     exit(rc);
  158. }
  159.  
  160.  
  161. void openall(void)
  162. {
  163.     KeymapBase = OpenLibrary("keymap.library", 37);
  164.     if (KeymapBase == NULL)  closeout("Kickstart 2.0 required", RETURN_FAIL);
  165.  
  166.     CxBase = OpenLibrary("commodities.library", 37);
  167.     if (CxBase == NULL)  closeout("Kickstart 2.0 required", RETURN_FAIL);
  168.  
  169.     InputEvent = AllocMem(sizeof(struct InputEvent), MEMF_CLEAR);
  170.     if (InputEvent == NULL)  closeout("Can't allocate input event",RETURN_FAIL);
  171. }
  172.  
  173.  
  174. void closeall()
  175. {
  176.     if (InputEvent)    FreeMem(InputEvent, sizeof(struct InputEvent));
  177.     if (CxBase)        CloseLibrary(CxBase);
  178.     if (KeymapBase)    CloseLibrary(KeymapBase);
  179. }
  180.  
  181.  
  182. void closeout(UBYTE *errstring, LONG rc)
  183. {
  184.     if(*errstring)     printf("%s\n",errstring);
  185.     closeall();
  186.     exit(rc);
  187. }
  188.